package com.example.controller;

import com.example.entity.Driver;
import com.example.entity.Vehicle;
import com.example.entity.VehicleType;
import com.example.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/drivers")
public class DriverController {

    @Autowired
    private DriverService driverService;

    @Operation(summary = "司机加盟申请", description = "处理司机的加盟申请，检查信息并保存到数据库")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "申请成功或失败信息",
                    content = @Content(schema = @Schema(implementation = String.class)))
    })
    @PostMapping
    public String applyForJoining(@RequestBody Map<String, Object> request) {
        //Map<String, Object> request 是一个 Map 对象，用于存储从请求体中获取的司机和车辆信息。
        String name = request.get("name").toString();
        String licenseNumber = request.get("licenseNumber").toString();
        String joinType = request.get("joinType").toString(); // 加盟类型：COMPANY_VEHICLE, DRIVER_VEHICLE, AUTONOMOUS_VEHICLE
        
        String vehicleLicensePlate = "";
        String model = "";
        VehicleType type = null;
        int seats = 0;
        boolean canCarpool = false;
        boolean isAutonomous = false;
        boolean companyOwned = false;
        
        // 根据不同的加盟类型设置车辆信息
        if ("COMPANY_VEHICLE".equals(joinType)) {
            // 公司提供车辆，司机本人当司机
            companyOwned = true;
            isAutonomous = false;
            vehicleLicensePlate = "CP" + System.currentTimeMillis(); // 为公司车辆生成临时车牌号
        } else if ("DRIVER_VEHICLE".equals(joinType)) {
            // 司机自带车辆加盟
            companyOwned = false;
            isAutonomous = false;
            vehicleLicensePlate = request.get("vehicleLicensePlate").toString();
        } else if ("AUTONOMOUS_VEHICLE".equals(joinType)) {
            // 自动驾驶类车辆
            companyOwned = false;
            isAutonomous = true;
            vehicleLicensePlate = request.get("vehicleLicensePlate").toString();
        }
        
        model = request.get("model").toString();
        String typeStr = request.get("type").toString();
        type = VehicleType.valueOf(typeStr);
        seats = Integer.parseInt(request.get("seats").toString());
        canCarpool = Boolean.parseBoolean(request.get("canCarpool").toString());

        Vehicle vehicle = new Vehicle();
        vehicle.setLicensePlate(vehicleLicensePlate);
        vehicle.setModel(model);
        vehicle.setType(type);
        vehicle.setSeats(seats);
        vehicle.setCanCarpool(canCarpool);
        vehicle.setIsAutonomous(isAutonomous);
        vehicle.setCompanyOwned(companyOwned);
        vehicle.setLatitude(0); // 初始经纬度
        vehicle.setLongitude(0);
        Driver driver = new Driver(name, licenseNumber,vehicle);

        return driverService.applyForJoining(driver, vehicle);
    }

    @Operation(summary = "获取所有司机信息", description = "返回公司所有司机的信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取所有司机信息",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Driver.class)) })
    })
    @GetMapping
    public List<Driver> getAllDrivers() {
        List<Driver> drivers = driverService.getAllDrivers();
        return drivers;
    }
}